﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;
using System.Configuration;
using Ext.Net;
using System.Data;
using Newtonsoft.Json;
using System.IO;
using System.Text;


namespace TCDC_System.Content.MasterData
{
    public partial class p_tcdc_trn_payment_info : SystemPage.UIPage
    {
        private tcdc_trn_payment_info objPaymentInfo;
        protected void Page_Init(object sender, EventArgs e)
        {
            this.PaymentInfoAdd1.GridStore = StorePaymentInfo;
        }

        protected void Page_Load(object sender, EventArgs e)
        {

            objPaymentInfo = new tcdc_trn_payment_info();
            objPaymentInfo.strConn = ConfigurationManager.AppSettings["ConnString"];
            if (!X.IsAjaxRequest)
            {
                StorePaymentInfo.DataSource = GetPaymentInfo();
                StorePaymentInfo.DataBind();
            }
        }

        protected DataTable GetPaymentInfo()
        {
            DataTable dt = objPaymentInfo.getByObject(new tcdc_trn_payment_info());
            return dt;
        }

        protected void AddPaymentInfo(object sender, DirectEventArgs e)
        {
            PaymentInfoAdd1.Show();
        }

        protected void UpdatePaymentInfo(object sender, DirectEventArgs e)
        {
            string json = e.ExtraParams["PaymentInfoValue"];
            Dictionary<string, string>[] workingdays = JSON.Deserialize<Dictionary<string, string>[]>(json);
            string payment_info_id = string.Empty;
            string invoice_no = string.Empty;
            string payment_date = string.Empty;
            string user_id = string.Empty;
            string payment_name = string.Empty;
            string payment_address = string.Empty;
            string province = string.Empty;
            string post_code = string.Empty;
            string payment_method = string.Empty;
            string bank_name = string.Empty;
            string bank_branch = string.Empty;
            string budget_year = string.Empty;
            string payment_no = string.Empty;
            string cancel_status = string.Empty;
            string cancel_desc = string.Empty;
            string create_date = string.Empty;
            string create_by = string.Empty;
            string update_date = string.Empty;
            string update_by = string.Empty;
            string site_id = string.Empty;

            foreach (Dictionary<string, string> row in workingdays)
            {
                payment_info_id = row["payment_info_id"].ToString();
                invoice_no = row["invoice_no"].ToString();
                payment_date = row["payment_date"].ToString();
                user_id = row["user_id"].ToString();
                payment_name = row["payment_name"].ToString();
                payment_address = row["payment_address"].ToString();
                province = row["province"].ToString();
                post_code = row["post_code"].ToString();
                payment_method = row["payment_method"].ToString();
                bank_name = row["bank_name"].ToString();
                bank_branch = row["bank_branch"].ToString();
                budget_year = row["budget_year"].ToString();
                payment_no = row["payment_no"].ToString();
                cancel_status = row["cancel_status"].ToString();
                cancel_desc = row["cancel_desc"].ToString();
                create_date = row["create_date"].ToString();
                create_by = row["create_by"].ToString();
                update_date = row["update_date"].ToString();
                update_by = row["update_by"].ToString();
                site_id = row["site_id"].ToString();
            }

            objPaymentInfo.payment_info_id = Convert.ToInt32(payment_info_id);
            objPaymentInfo.invoice_no = invoice_no;
            objPaymentInfo.payment_date = payment_date;
            objPaymentInfo.user_id = Convert.ToInt32(user_id);
            objPaymentInfo.payment_name = payment_name;
            objPaymentInfo.payment_address = payment_address;
            objPaymentInfo.province = province;
            objPaymentInfo.post_code = post_code;
            objPaymentInfo.payment_method = payment_method;
            objPaymentInfo.bank_name = bank_name;
            objPaymentInfo.bank_branch = bank_branch;
            objPaymentInfo.budget_year = Convert.ToInt32(budget_year);
            objPaymentInfo.payment_no = Convert.ToInt32(payment_no);
            objPaymentInfo.cancel_status = cancel_status;
            objPaymentInfo.cancel_desc = cancel_desc;
            objPaymentInfo.create_date = create_date;
            objPaymentInfo.create_by = Convert.ToInt32(create_by);
            objPaymentInfo.update_date = update_date;
            objPaymentInfo.update_by = Convert.ToInt32(update_by);
            objPaymentInfo.site_id = Convert.ToInt32(site_id);
            objPaymentInfo.updateData(objPaymentInfo);
            StorePaymentInfo.DataSource = GetPaymentInfo();
            StorePaymentInfo.DataBind();
        }

        protected void DeletePaymentInfo(object sender, DirectEventArgs e)
        {
            this.ConfirmDeletePaymentInfo();
        }

        protected void StorePaymentInfo_Refresh(object sender, StoreRefreshDataEventArgs e)
        {
            StorePaymentInfo.DataSource = GetPaymentInfo();
            StorePaymentInfo.DataBind();
        }

        protected void ExportExcelPaymentInfo(object sender, EventArgs e)
        {
            string json = GridData.Value.ToString();

            DataTable dt = (DataTable)JsonConvert.DeserializeObject(json, (typeof(DataTable)));

            if (dt.Rows.Count > 0)
            {
                Response.Clear();
                Response.AddHeader("content-disposition", "attachment;filename=PaymentInfo.xls");
                Response.ContentType = "application/vnd.ms-excel";
                StringWriter stringWriter = new StringWriter();
                HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWriter);
                DataGrid dataExportExcel = new DataGrid();
                dataExportExcel.DataSource = dt;
                dataExportExcel.DataBind();
                dataExportExcel.RenderControl(htmlWrite);
                StringBuilder sbResponseString = new StringBuilder();
                sbResponseString.Append("<html xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns=\"http://www.w3.org/TR/REC-html40\"> <head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"></head> <body>");
                sbResponseString.Append(stringWriter + "</body></html>");
                sbResponseString.Replace("&nbsp;", "&#160;");
                Response.Write(sbResponseString.ToString());
                Response.End();
            }
        }
        [DirectMethod(Namespace = "MyNameSpace")]
        public void ConfirmDeletePaymentInfo()
        {
            X.Msg.Confirm("Confirm", "ยืนยันการลบ ?", new MessageBoxButtonsConfig
            {
                Yes = new MessageBoxButtonConfig
                {
                    Handler = "MyNameSpace.DeleteDataPaymentInfo()",
                    Text = "Yes"
                },
                No = new MessageBoxButtonConfig { Text = "No" }
            }).Show();
        }
        [DirectMethod(Namespace = "MyNameSpace")]
        public void DeleteDataPaymentInfo()
        {
            RowSelectionModel sm = this.GridPanelPaymentInfo.SelectionModel.Primary as RowSelectionModel;
            string payment_info_id = string.Empty;

            foreach (SelectedRow row in sm.SelectedRows)
            {
                payment_info_id = row.RecordID;
            }
            objPaymentInfo.deleteData(payment_info_id);
            StorePaymentInfo.DataSource = GetPaymentInfo();
            StorePaymentInfo.DataBind();
        }
    }
}

